package com.artisan.domain.service;

import com.artisan.domain.dto.MenuDto;
import com.artisan.domain.entity.MMenu;
import com.artisan.domain.repositoryInterface.IMenuRepository;
import com.artisan.domain.serviceInterface.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 快捷菜单业务类
 */
@Service
public class MenuService implements IMenuService {
    @Autowired
    private IMenuRepository menuRepository;
    @Override
    public List<MenuDto> findList(int[] ids) {

        String[] idss = (String[]) Arrays.stream(ids).mapToObj(String::valueOf).toArray();
        List<MMenu> models = this.menuRepository.find(String.join(",", idss));

        List<MenuDto> dtos = new ArrayList<>();
        models.forEach(model->{
            MenuDto dto = new MenuDto();
            dto.setId(model.getId());
            dto.setName(model.getName());
            dto.setParentId(model.getParentId());

            dtos.add(dto);
        });

        return dtos;
    }

    @Override
    public List<MenuDto> findList() {
        List<MMenu> models = this.menuRepository.findAll();

        List<MenuDto> dtos = new ArrayList<>();
        models.forEach(model->{
            MenuDto dto = new MenuDto();
            dto.setId(model.getId());
            dto.setName(model.getName());
            dto.setParentId(model.getParentId());

            dtos.add(dto);
        });

        return dtos;
    }
}
